package com.ofek.applicationbase.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class TimeUtils {

	public static long getToday() {
        final Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);
        return today.getTimeInMillis();
	}

	public static long inXDays(final int i) {
		final Calendar today = getTodayCal();
		today.add(Calendar.DAY_OF_MONTH, i);
        return today.getTimeInMillis();
	}
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	public static String formatRange(final Calendar start, final Calendar end) {
		if (start.get(Calendar.MONTH)==end.get(Calendar.MONTH)) 
			return dateFormat.format(start.getTimeInMillis()).subSequence(0, 2)+"-"+dateFormat.format(end.getTimeInMillis());
		if (start.get(Calendar.YEAR)==end.get(Calendar.YEAR)) 
			return dateFormat.format(start.getTimeInMillis()).subSequence(0, 5)+"-"+dateFormat.format(end.getTimeInMillis());
		return dateFormat.format(start.getTimeInMillis())+" - "+dateFormat.format(end.getTimeInMillis());
	}

	public static String format(final Calendar todayShowing) {
		return dateFormat.format(todayShowing.getTimeInMillis());
	}

	public static Calendar getTodayCal() {
		final Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);
        return today;
	}

	public static Calendar getWeekStartCal() {
		final Calendar thisWeekS = getTodayCal();
        thisWeekS.set(Calendar.DAY_OF_WEEK, thisWeekS.getFirstDayOfWeek());
        return thisWeekS;
	}

	public static long getWeekStart() {
		final Calendar thisWeekS = getTodayCal();
        thisWeekS.set(Calendar.DAY_OF_WEEK, thisWeekS.getFirstDayOfWeek());
        return thisWeekS.getTimeInMillis();
	}

	public static String formatRange(final long start, final long end) {
		final Calendar startCal = Calendar.getInstance();
		startCal.setTimeInMillis(start);
		final Calendar endCal = Calendar.getInstance();
		endCal.setTimeInMillis(end);
		return TimeUtils.formatRange(startCal, endCal);
	}

	public static String format(final long start) {

		return format(toCal(start));
	}

	private static Calendar toCal(final long start) {
		final Calendar startCal = Calendar.getInstance();
		startCal.setTimeInMillis(start);
		return startCal;
	}

}
